package cn.passlink.bletoothchat.Adapter;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.List;

import cn.passlink.bletoothchat.Entity.Friends;
import cn.passlink.bletoothchat.Entity.Groups;
import cn.passlink.bletoothchat.R;
import cn.passlink.bletoothchat.SqlTables.MyDatabaseHelper;
import cn.passlink.bletoothchat.mActivity.FriendTalkingActivity;
import cn.passlink.bletoothchat.mActivity.GroupTalkingActivity;

public class ScanGroupsAdapter extends BaseAdapter {
    private Context context;
    private List<Groups> groupsList;
    private SQLiteDatabase database;
    private MyDatabaseHelper databaseHelper;


    private List<Groups> data;  // 定义数据源列表变量

    public ScanGroupsAdapter(Context context, List<Groups> groupsList) {
        this.context = context;
        this.groupsList = groupsList;

        // 初始化数据库辅助类
        databaseHelper = new MyDatabaseHelper(context,2);
        // 获取可写入的数据库实例
        database = databaseHelper.getWritableDatabase();
    }

    @Override
    public int getCount() {
        return groupsList.size();
    }

    @Override
    public Object getItem(int position) {
        return groupsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.scan_groups_item, parent, false);
            holder = new ViewHolder();
            holder.groupnameTextView = convertView.findViewById(R.id.scan_group_name_TextView);
            holder.groupidTextView = convertView.findViewById(R.id.scan_group_id_TextView);
            holder.membersAmountTextView = convertView.findViewById(R.id.scan_group_members_TextView);
            holder.Connect=convertView.findViewById(R.id.scan_groups_item_connect);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Groups groups = groupsList.get(position);
        holder.groupnameTextView.setText( groups.getGroupname());
        holder.groupidTextView.setText("ID:"+groups.getGroupid());
        holder.membersAmountTextView.setText("all:"+groups.getMembers_amount());




        holder.Connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理Connect按钮的点击事件
                // 编写您想要执行的代码逻辑
                Groups mGroups = groupsList.get(position);
                // 处理Connect按钮的点击事件
                // 编写您想要执行的代码逻辑
                goToTargetActivity(mGroups.getGroupname(),mGroups.getGroupid());
                Toast.makeText(context, "尝试连接中。。。", Toast.LENGTH_SHORT).show();
            }
        });
        // 设置点击监听器
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理点击列表项的逻辑
                // 示例代码：显示一个Toast消息
                Toast.makeText(context, "Clicked item at position: " + position, Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }

    static class ViewHolder {
        TextView groupnameTextView;
        TextView groupidTextView;
        TextView membersAmountTextView;
        ImageView Connect;
    }

    public void goToTargetActivity(String whoName,String groupId) {
        // 创建 Intent
        Intent intent = new Intent(context, GroupTalkingActivity.class);
        // 传递数据（可选）
        intent.putExtra("groupName", whoName);
        intent.putExtra("groupid", groupId);
        // 启动 Activity
        context.startActivity(intent);
    }
}
